data structures dp geometry *2400

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

struct Rect {
	ll x, y, a;
	bool operator<(Rect B) { return x < B.x; }
};

Rect a[1000001];
ll dp[1000001];

double slope(int i, int j) {
	return (double)(dp[i] - dp[j]) / (a[i].x - a[j].x);
}

int main() {
	iostream::sync_with_stdio(false);
	cin.tie(0);
	ll n;
	cin >> n;
	for (int i = 1; i <= n; i++) { cin >> a[i].x >> a[i].y >> a[i].a; }
	sort(a + 1, a + n + 1);

	deque<ll> q;
	q.push_back(0);
	for (int i = 1; i <= n; i++) {
		while (q.size() > 1 && slope(q[0], q[1]) >= a[i].y) q.pop_front();

		ll j = q.front();
		dp[i] =
		    max(dp[i - 1], a[i].x * a[i].y - a[i].a + dp[j] - a[j].x * a[i].y);

		while (q.size() > 1 &&
		       slope(q[q.size() - 2], q.back()) <= slope(q.back(), i))
			q.pop_back();
		q.push_back(i);
	}

	cout << dp[n];
	return 0;
}


Comments

Submit
0 Comments
More Questions

87A - Trains
664A - Complicated GCD
1635D - Infinite Set
1462A - Favorite Sequence
1445B - Elimination
1656C - Make Equal With Mod
567A - Lineland Mail
1553A - Digits Sum
1359B - New Theatre Square
766A - Mahmoud and Longest Uncommon Subsequence
701B - Cells Not Under Attack
702A - Maximum Increase
1656D - K-good
1426A - Floor Number
876A - Trip For Meal
1326B - Maximums
1635C - Differential Sorting
961A - Tetris
1635B - Avoid Local Maximums
20A - BerOS file system
1637A - Sorting Parts
509A - Maximum in Table
1647C - Madoka and Childish Pranks
689B - Mike and Shortcuts
379B - New Year Present
1498A - GCD Sum
1277C - As Simple as One and Two
1301A - Three Strings
460A - Vasya and Socks
1624C - Division by Two and Permutation